package com.diligent.cloud.sfc.service.impl;

import com.canny.commons.model.Codes;
import com.canny.commons.model.ListResult;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.model.Result;
import com.canny.commons.page.IPage;
import com.canny.commons.page.Pagination;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.diligent.cloud.logistics.container.model.*;
import com.diligent.cloud.logistics.container.service.*;
import com.diligent.cloud.primary.data.feign.service.mdm.MdmBaseFeignService;
import com.diligent.cloud.primary.data.model.Period;
import com.diligent.cloud.sfc.config.OauthConfig;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WorkshopTransportService;
import com.diligent.cloud.sfc.util.ConverterUtil;
import com.diligent.cloud.web.model.PageAndCondition;
import com.diligent.cloud.workorder.model.WoLots;
import com.diligent.cloud.workorder.model.WoOrder;
import com.diligent.cloud.workorder.service.WoLotsFeignService;
import com.diligent.cloud.workorder.service.WoOrderFeignService;
import com.diligent.foundation.data.model.v2.Condition;
import com.google.common.base.Preconditions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * WorkshopTransportServiceImpl class
 *
 * @author xf107278
 * @date 3/30/2020
 */
@Service
public class WorkshopTransportServiceImpl implements WorkshopTransportService {

    private LcmBoxLedgerFeignService lcmBoxLedgerFeignService;
    private final LcmBoxFillFeignService lcmBoxFillFeignService;
    private LcmWeightApplyFeignService lcmWeightApplyFeignService;
    private LcmTransportFeignService lcmTransportFeignService;
    private LcmMoveApplyFeignService lcmMoveApplyFeignService;
    private ISfcService sfcService;
    private final MdmBaseFeignService mdmBaseFeignService;

    private final WoOrderFeignService orderFeignService;

    private final WoLotsFeignService lotsFeignService;

    private final OauthConfig.RequestParams requestParams;

    private final LcmBoxMatFeignService lcmBoxMatFeignService;

    private final LcmBoxFillDetailFeignService lcmBoxFillDetailFeignService;

    public WorkshopTransportServiceImpl(LcmBoxLedgerFeignService lcmBoxLedgerFeignService,
                                        LcmBoxFillFeignService lcmBoxFillFeignService,
                                        LcmWeightApplyFeignService lcmWeightApplyFeignService,
                                        LcmTransportFeignService lcmTransportFeignService,
                                        LcmMoveApplyFeignService lcmMoveApplyFeignService, ISfcService sfcService,
                                        MdmBaseFeignService mdmBaseFeignService, WoOrderFeignService orderFeignService, WoLotsFeignService lotsFeignService,
                                        OauthConfig.RequestParams requestParams, LcmBoxMatFeignService lcmBoxMatFeignService, LcmBoxFillDetailFeignService lcmBoxFillDetailFeignService) {
        this.lcmBoxLedgerFeignService = lcmBoxLedgerFeignService;
        this.lcmBoxFillFeignService = lcmBoxFillFeignService;
        this.lcmWeightApplyFeignService = lcmWeightApplyFeignService;
        this.lcmTransportFeignService = lcmTransportFeignService;
        this.lcmMoveApplyFeignService = lcmMoveApplyFeignService;
        this.sfcService = sfcService;
        this.mdmBaseFeignService = mdmBaseFeignService;
        this.orderFeignService = orderFeignService;
        this.lotsFeignService = lotsFeignService;
        this.requestParams = requestParams;
        this.lcmBoxMatFeignService = lcmBoxMatFeignService;
        this.lcmBoxFillDetailFeignService = lcmBoxFillDetailFeignService;
    }

    @Override
    public ResponseEntity<Result> searchTransport(String code, String systemId) {
        PageAndCondition pageAndCondition = new PageAndCondition();

        Pagination pagination = new Pagination(1L, 10L);
        Condition codeCondition = new Condition("code", "eq", code, null);
        Condition systemIdCondition = new Condition("systemId", "eq", systemId, null);
        List<Condition> conditions = new ArrayList<>();
        conditions.add(codeCondition);
        conditions.add(systemIdCondition);

        pageAndCondition.setPagination(pagination);
        pageAndCondition.setConditions(conditions);
        ResponseEntity<com.diligent.cloud.primary.data.model.Result> search =
                lcmBoxLedgerFeignService.search(pageAndCondition);
        if (search.getBody()!=null){
            IPage<LcmBoxLedger> lcmBoxLedgerIPage = (IPage<LcmBoxLedger>) search.getBody().getData();

            return ResponseEntity.ok(new ObjectResult<>(lcmBoxLedgerIPage.getRecords()));
        }
        return  ResponseEntity.ok(new ObjectResult<>());
    }

    @Override
    public ResponseEntity<Result> addTransport(LcmTransport lcmTransport, Principal principal) {
        LcmTransportMaster lcmTransportMaster = lcmTransport.getLcmTransportMaster();


        Long periodId = sfcService.getPeriodId();
        lcmTransportMaster.setPeriodId(periodId);
        lcmTransportMaster.setCompanyId(requestParams.getCompanyId());
        lcmTransportMaster.setSystemId(requestParams.getSystemId());
        for (LcmTransportDetail lcmTransportDetail : lcmTransport.getLcmTransportDetail()) {
            lcmTransportDetail.setEnterName(principal.getName());
            lcmTransportDetail.setEnterCode(principal.getName());
            lcmTransportDetail.setEnterTime(new Date());
            lcmTransportDetail.setCompanyId(requestParams.getCompanyId());
            lcmTransportDetail.setSystemId(requestParams.getSystemId());
        }
        for (LcmTransportMat lcmTransportMat : lcmTransport.getLcmTransportMat()) {
            lcmTransportMat.setEnterCode(principal.getName());
            lcmTransportMat.setEnterName(principal.getName());
            lcmTransportMat.setEnterTime(new Date());
            lcmTransportMat.setCompanyId(requestParams.getCompanyId());
            lcmTransportMat.setSystemId(requestParams.getSystemId());
        }

        com.diligent.cloud.primary.data.model.Result result = lcmTransportFeignService.add(lcmTransport).getBody();
        return ResponseEntity.ok(ConverterUtil.ConverterByResult(result));
    }

    @Override
    @Transactional
    @LcnTransaction
    public ResponseEntity<ObjectResult> addBoxFill(LcmBoxFill lcmBoxFill, Principal principal) {
        Preconditions.checkNotNull(lcmBoxFill.getLcmBoxFillMaster().getBoxCode(), "框号boxCode不可以为null");

        Long periodId = sfcService.getPeriodId();
        LcmBoxFillMaster lcmBoxFillMaster = lcmBoxFill.getLcmBoxFillMaster();
        lcmBoxFillMaster.setEnterCode(principal.getName());
        lcmBoxFillMaster.setEnterName(principal.getName());
        lcmBoxFillMaster.setEnterTime(new Date());
        lcmBoxFillMaster.setSystemId(requestParams.getSystemId());
        lcmBoxFillMaster.setCompanyId(requestParams.getCompanyId());
        lcmBoxFillMaster.setPeriodId(periodId);
        lcmBoxFillMaster.setFormId(1L);

        LcmBoxFillDetail lcmBoxFillDetail = lcmBoxFill.getLcmBoxFillDetail();
        lcmBoxFillDetail.setEnterTime(new Date());
        lcmBoxFillDetail.setEnterCode(principal.getName());
        lcmBoxFillDetail.setEnterName(principal.getName());
        lcmBoxFillDetail.setSystemId(requestParams.getSystemId());
        lcmBoxFillDetail.setCompanyId(requestParams.getCompanyId());
        lcmBoxFillDetail.setPeriodId(periodId);

        //产出装框
        ResponseEntity<com.diligent.cloud.primary.data.model.Result> boxFillResult =
                lcmBoxFillFeignService.handingMaterial(lcmBoxFill);

        //装框保存生产扎
        WoLots woLots = new WoLots();
        woLots.setBoxCode(lcmBoxFill.getLcmBoxFillMaster().getBoxCode());
        if (boxFillResult.getBody()!=null && boxFillResult.getBody().getData()!=null){
            LcmBoxFill lcmBoxFill1 = (LcmBoxFill) boxFillResult.getBody().getData();
            woLots.setFillBoxDocCode(lcmBoxFill1.getLcmBoxFillMaster().getDocCode());
        }
        ResponseEntity<com.diligent.cloud.primary.data.model.Result> orderResult =
                orderFeignService.selectById(Long.parseLong(lcmBoxFillDetail.getRefWoOrder()));
        Preconditions.checkNotNull(orderResult.getBody(),"查询不到工单信息");
        WoOrder woOrder = (WoOrder) orderResult.getBody().getData();
        woLots.setWoOrder(woOrder.getWoOrder());
        woLots.setDigit(lcmBoxFillDetail.getDigit());
        woLots.setSystemId(requestParams.getSystemId());
        woLots.setCompanyId(requestParams.getCompanyId());
        woLots.setPeriodId(periodId);

        lotsFeignService.insert(woLots);

        ObjectResult result;
        if (boxFillResult.getBody().getCode() == 0) {
            result = new ObjectResult();
        } else {
            result = new ObjectResult();
            result.modifyCode(Codes.fail.getCode(), Codes.fail.getMsg());
        }
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity<Result> addWeightApply(LcmWeightApply lcmWeightApply, Principal principal) {


        LcmWeightApplyMaster weightApplyMaster = lcmWeightApply.getLcmWeightApplyMaster();
        Long periodId = sfcService.getPeriodId();
        weightApplyMaster.setEnterCode(principal.getName());
        weightApplyMaster.setEnterName(principal.getName());
        weightApplyMaster.setEnterTime(new Date());
        weightApplyMaster.setSystemId(requestParams.getSystemId());
        weightApplyMaster.setCompanyId(requestParams.getCompanyId());
        weightApplyMaster.setPeriodId(periodId);

        for (LcmWeightApplyDetail lcmWeightApplyDetail : lcmWeightApply.getLcmWeightApplyDetail()) {

            lcmWeightApplyDetail.setEnterCode(principal.getName());
            lcmWeightApplyDetail.setEnterName(principal.getName());
            lcmWeightApplyDetail.setEnterTime(new Date());
            lcmWeightApplyDetail.setSystemId(requestParams.getSystemId());
            lcmWeightApplyDetail.setCompanyId(requestParams.getCompanyId());
            lcmWeightApplyDetail.setPeriodId(periodId);

        }


        return ResponseEntity.ok(ConverterUtil.ConverterByResult(lcmWeightApplyFeignService.applyForWeight(lcmWeightApply).getBody()));
    }

    @Override
    public ResponseEntity<Result> addMoveApply(LcmMoveApply moveApply, Principal principal) {
        long periodId = mdmBaseFeignService.getPeriodId().getBody().getData().getId().longValue();
        LcmMoveApplyMaster lcmMoveApplyMaster = moveApply.getLcmMoveApplyMaster();
        lcmMoveApplyMaster.setEnterTime(new Date());
        lcmMoveApplyMaster.setEnterName(principal.getName());
        lcmMoveApplyMaster.setEnterCode(principal.getName());
        lcmMoveApplyMaster.setSystemId(requestParams.getSystemId());
        lcmMoveApplyMaster.setCompanyId(requestParams.getCompanyId());
        lcmMoveApplyMaster.setPeriodId(periodId);

        for (LcmMoveApplyDetail moveApplyDetail : moveApply.getLcmMoveApplyDetail()) {
            moveApplyDetail.setEnterTime(new Date());
            moveApplyDetail.setEnterName(principal.getName());
            moveApplyDetail.setEnterCode(principal.getName());
            moveApplyDetail.setSystemId(requestParams.getSystemId());
            moveApplyDetail.setCompanyId(requestParams.getCompanyId());
        }
        ResponseEntity<com.diligent.cloud.primary.data.model.Result> resultResponseEntity =
                lcmMoveApplyFeignService.applyForMove(moveApply);
        if (resultResponseEntity.getBody()!=null ){
            return ResponseEntity.ok(new ObjectResult<>(resultResponseEntity.getBody().getData()));
        }else {
            ObjectResult result = new ObjectResult();
            result.modifyCode(Codes.fail.getCode(),"移动申请失败");
            return ResponseEntity.ok(result);
        }
    }

    @Override
    public ResponseEntity<ListResult> getIdleBox(String machCode, Long pageIndex, Long pageSize) {
        IPage<LcmBoxLedger> idleBox = sfcService.getIdleBox(machCode, pageIndex, pageSize);
        ListResult result ;
        if (idleBox != null) {
            result = new ListResult(idleBox.getRecords());
            result.setMsg(idleBox.getTotal().toString());
        } else {
            result = new ListResult(new ArrayList());
            result.setMsg("0");
        }
        return ResponseEntity.ok(result);
    }

    @Override
    @Transactional
    @LcnTransaction
    public ResponseEntity<Result> updateBoxMat(Long matId, Long lotId, String lotCode,Long fillDetailId) {
        LcmBoxMat lcmBoxMat = new LcmBoxMat();
        lcmBoxMat.setId(matId);
        lcmBoxMat.setLotCode(lotCode);
        lcmBoxMat.setLotId(lotId);

        LcmBoxFillDetail lcmBoxFillDetail = new LcmBoxFillDetail();
        lcmBoxFillDetail.setId(fillDetailId);
        lcmBoxFillDetail.setLotCode(lotCode);
        lcmBoxFillDetail.setLotId(lotId);
        lcmBoxFillDetailFeignService.update(lcmBoxFillDetail);

        return ResponseEntity.ok(ConverterUtil.ConverterByResult(lcmBoxMatFeignService.update(lcmBoxMat).getBody()));
    }

}
